package com.deyang.mycommon.ui.activity.basics;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.deyang.mycommon.R;
import com.deyang.mycommon.adapter.SelectImagesAdapter;
import com.deyang.mycommon.application.MyApplication;
import com.deyang.mycommon.config.ConfigRes;
import com.deyang.mycommon.entity.SelectImageItem;
import com.deyang.mycommon.util.ImageUtil;
import com.deyang.mycommon.util.ToastUtil;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;

import org.devio.takephoto.app.TakePhoto;
import org.devio.takephoto.app.TakePhotoActivity;
import org.devio.takephoto.model.TImage;
import org.devio.takephoto.model.TResult;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 王德阳
 * @data 创建时间: 2017/12/19 0019
 * @Description 选择图片
 */
public class SelectImagesActivity extends TakePhotoActivity {


    @BindView(R.id.rv_cardImages)
    RecyclerView rvCardImages;

    private SelectImagesAdapter adapter;
    private List<SelectImageItem> list = new ArrayList<>();

    private int multipleSize = 9;  //一次多选个数

    private MyApplication mApp;
    private TakePhoto takePhoto;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_images);
        ButterKnife.bind(this);

        mApp = (MyApplication) getApplication();
        mApp.getOpenActivityList().add(this);

        adapter = new SelectImagesAdapter(list);
        rvCardImages.setLayoutManager(new GridLayoutManager(this, 3));
        rvCardImages.setNestedScrollingEnabled(false);
        rvCardImages.setFocusable(false);
        rvCardImages.setAdapter(adapter);


        takePhoto = getTakePhoto();
        setListener();

        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        // TODO: 2018/7/14 重新加载数据
        SelectImageItem item = new SelectImageItem();
        item.setCanAdd(true);

        adapter.addData(item);
    }


    public void setListener() {

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                clickItem(position);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.ll_delete:
                        adapter.remove(position);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * 查看图片 /  上传图片
     *
     * @param position
     */
    private void clickItem(int position) {
        SelectImageItem info = list.get(position);
        if (info.isCanAdd()) {
            final String[] stringItems = {"拍照", "从相册"};
            final ActionSheetDialog dialog = new ActionSheetDialog(SelectImagesActivity.this, stringItems, null);
            dialog.title("选择图片")
                    .titleTextSize_SP(14f)
                    .show();
            dialog.setOnOperItemClickL(new OnOperItemClickL() {
                @Override
                public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ImageUtil.configCompress(takePhoto, true);
                    ImageUtil.configTakePhotoOption(takePhoto);
                    if (position == 0) {
                        File file = new File(ConfigRes.Image + "/" + System.currentTimeMillis() + ".jpg");
                        if (!file.getParentFile().exists()) {
                            file.getParentFile().mkdirs();
                        }
                        Uri imageUri = Uri.fromFile(file);
                        takePhoto.onPickFromCapture(imageUri);
                    } else {
                        takePhoto.onPickMultiple(multipleSize);
                    }
                    dialog.dismiss();
                }
            });
        } else {
            String images = "";
            if (!info.isCanAdd()) {
                for (SelectImageItem resouseInfo : list) {
                    if (!resouseInfo.isCanAdd()) {
                        images += resouseInfo.getSketchPath() + ConfigRes.ImageShowDivision;
                    }
                }
            }
            ImageUtil.showImages(SelectImagesActivity.this, images, position);
        }
    }


    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        ToastUtil.showInfo(msg);
    }

    /**
     * 选择图片返回   添加数据
     *
     * @param result
     */
    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);

        ArrayList<TImage> images = result.getImages();
        for (int i = 0; i < images.size(); i++) {
            String compressPath = images.get(i).getCompressPath();
            SelectImageItem item = new SelectImageItem();
            item.setCanAdd(false);
            item.setSketchPath(compressPath);
            list.add(item);
        }
        loadData();
    }
}
